package com.example.demo.controller;

import com.example.demo.pojo.Job;
import com.example.demo.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class JobController {

    @Autowired
    private JobService jobService;

    @RequestMapping("/jobs")
    public String list(Model model){
        model.addAttribute("jobs",jobService.getAllJob());
        return "job/job_list";
    }

    @GetMapping("/addjob")
    public String toAddjob(){ return "job/add";}

    @PostMapping("/job")
    public String addJob(Job job){
        jobService.addNewJob(job);
        return "redirect:/jobs";
    }

    @GetMapping("job/{id}")
    public String toUpdateJob(@PathVariable("id")Integer id, Model model){
        model.addAttribute("job",jobService.selectJobById(id));
        return "job/update";
    }

    @PostMapping("/updatejob")
    public String updateJob(Job job){
        jobService.updateJob(job);
        return "redirect:/jobs";
    }

    @GetMapping("/deljob/{id}")
    public String deleteJob(@PathVariable("id")int id){
        jobService.deleteJobById(id);
        return "redirect:/jobs";
    }
}
